package tij4.concurrent.exercise.ex10;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class Practice01 implements Callable<Integer> {

    private int num;
    ExecutorService executorService = Executors.newSingleThreadExecutor();

    public Integer fib(int x){
        if(x<=2) return 1;
        return fib(x-2) + fib(x-1);
    }

    @Override
    public Integer call() throws Exception {
        int sum = 0;
        for (int i = 1; i <= num; i++) {
            sum += fib(i);
        }
        return sum;
    }

    public Future<Integer> runTask(Integer num){
        this.num = num;
        return executorService.submit(this);
    }
}
